﻿using System;
using System.Collections.Generic;

using System.Text;
using System.ComponentModel;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Manipulation;
using ITAP.ShprossesBuilder.Calculations.CoordinateTransformations;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.ShprossesPropertyGrid.ComponentsProxy
{
    public class ShprossConnectorProxy : Proxy
    {
        private ICoordinateTransformer coordinateTransformer;
        public ShprossConnectorProxy(ShprossConnector shprossConnector, MainControl mainControl) : base(mainControl)
        {
            this.shprossConnector = shprossConnector;
            this.coordinateTransformer = mainControl.ModelControl.CoordinateTransformer;

        }

        [Category("Общие свойства"), DisplayName("Прозрачность накладки (1-100)"), Description("Прозрачность накладки.")]
        public int SnapOnCapOpacity
        {
            get
            {
                SnapOnCap snapOnCap = shprossConnector.SnapOnCap;
                if (snapOnCap != null)
                    return snapOnCap.Opacity;
                else
                    return 0;
            }
            set
            {
                int opacity;
                if (value > 100)
                    opacity = 100;
                else if (value < 1)
                    opacity = 1;
                else
                    opacity = value;
                SnapOnCap snapOnCap = shprossConnector.SnapOnCap;
                if (snapOnCap != null)
                    snapOnCap.Opacity = opacity;
            }
        }

        private ShprossConnector shprossConnector;

        [Category("Общие свойства"), DisplayName("Артикул"), Description("Артикул соединителя.")]
        [ReadOnly(true)]
        public string Name
        {
            get { return shprossConnector.Article; }
            set { shprossConnector.Article = value; }
        }
        [Category("Положение"), DisplayName("X"), Description("Положение по X.")]
        public double X
        {
            get 
            {
                ModelPoint point = coordinateTransformer.TransformTo(mainControl.ModelContainer, shprossConnector.Position);
                return Math.Round(point.X, 1, MidpointRounding.ToEven);
            }
            set 
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint point = coordinateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(value, 0));
                shprossConnector.X = point.X;
            }
        }
        [Category("Положение"), DisplayName("Y"), Description("Положение по Y.")]
        public double Y
        {
            get 
            {
                ModelPoint point = coordinateTransformer.TransformTo(mainControl.ModelContainer, shprossConnector.Position);
                return Math.Round(point.Y, 1, MidpointRounding.ToEven);
            }
            set 
            {
                this.ExecutePropertyValueChangingCommand();
                ModelPoint point = coordinateTransformer.TransformFrom(mainControl.ModelContainer, new ModelPoint(0, value));
                shprossConnector.Y = point.Y;
            }
        }

        [Category("Положение"), DisplayName("Угол поворота"), Description("Угол поворота соединителя.")]
        public double Angle
        {
            get
            {
                return Math.Round(shprossConnector.Angle.Value * 180 / Math.PI, 1, MidpointRounding.ToEven);
            }
            set
            {
                this.ExecutePropertyValueChangingCommand();
                Angle newAngle = new Angle((value % 360) * Math.PI / 180);
                shprossConnector.Angle = newAngle;
            }
        }

    }
}
